using System;
using System.Xml;
using System.Net;
using System.IO;
using System.Text;
/*
				Unified.Network.Atom.AtomEngine engine = new Unified.Network.Atom.AtomEngine("USER", "PASS");
				Unified.Network.Atom.Entry entry = new Unified.Network.Atom.Entry("Test From Unified", "this is a test from unified", System.DateTime.Now);
				engine.PostEntry("https://www.blogger.com/atom/BLOGID", entry);
*/


namespace Unified.Network.Atom {


	public class AtomEngine {
		private string userName;
		private string password;
		private HttpStatusCode statusCode;

		public AtomEngine(string userName, string password) {
			this.userName = userName;
			this.password = password;
		}

		public XmlDocument GetFeed(string feedUri) {

			return CallAtomHost("GET",feedUri,null);

		}

		public XmlDocument PostEntry(string feedUri, Entry entry) {

			return CallAtomHost("POST",feedUri,entry);

		}

		public XmlDocument PutEntry(string feedUri, Entry entry) {

			return CallAtomHost("PUT",feedUri,entry);

		}

		private XmlDocument CallAtomHost(string method, string uri, Entry entry) {


			HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);

			req.Method = method;

			req.ProtocolVersion = new Version(1,1);

			//if(authenticationType==AuthenticationType.Basic) {

				req.Credentials = new NetworkCredential(userName,password);

			//}

			if(method=="POST"||method=="PUT") {

				req.ContentType = "application/atom+xml";

			}

			req.Accept = "application/atom+xml";

			req.PreAuthenticate = true;

			req.AllowWriteStreamBuffering = true;

			req.SendChunked = false;//for Blogger


			if(entry!=null) {

				WriteEntry(entry, req.GetRequestStream());

			}

			XmlDocument response = new XmlDocument();

			HttpWebResponse resp = null;

			try {

				resp = (HttpWebResponse)req.GetResponse();

			}

			catch(WebException ex) {

				resp = (HttpWebResponse)ex.Response;

			}

			finally {

				if(resp.StatusCode==HttpStatusCode.OK) {


					XmlTextReader reader = new XmlTextReader(resp.GetResponseStream());

					response.Load(reader);

					reader.Close();


				}


				statusCode = resp.StatusCode;

				resp.Close();


			}

			return response;

		}

		private void WriteEntry(Entry entry, Stream requestStream) {

			Encoding encoding = Encoding.UTF8;

			MemoryStream stream = new MemoryStream();

			XmlTextWriter writer = new XmlTextWriter(stream,encoding);

			writer.Formatting = Formatting.None;


			writer.WriteStartDocument(true);

			writer.WriteStartElement("entry", "http://purl.org/atom/ns#");

			writer.WriteStartElement("title");

			writer.WriteAttributeString("mode","escaped");

			writer.WriteAttributeString("type","text/plain");

			writer.WriteString(string.Format("{0}",entry.Title));

			writer.WriteEndElement();

			writer.WriteElementString("issued", string.Format("{0:yyyy-MM-ddTHH:mm:sszzzz}",entry.Issued));

			writer.WriteStartElement("generator");

			writer.WriteAttributeString("url","http://kevdaly.aspxconnection.com");

			writer.WriteString("Atomic Blogger");

			writer.WriteEndElement();

			writer.WriteStartElement("content");

			writer.WriteAttributeString("type","application/xhtml+xml");

			writer.WriteStartElement("div","http://www.w3.org/1999/xhtml");

			writer.WriteRaw(entry.Content);

			writer.WriteEndElement();


			writer.WriteEndElement();

			writer.WriteEndElement();

			writer.WriteEndDocument();


			writer.Flush();

			requestStream.Write(stream.GetBuffer(),0,(int)stream.Length);

			requestStream.Close();

			stream.Close();

			writer.Close();

		}

		public XmlDocument DeleteEntry(string feedUri) {

			return CallAtomHost("DELETE",feedUri,null);

		}

		public HttpStatusCode StatusCode {

			get {return statusCode;}

		}
/*
		public AuthenticationType AuthenticationType {

			set {authenticationType = value;}

			get {return authenticationType;}

		}
*/

	}

}